import numbers

import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from pytorch_wavelets import DWTForward
from timm.models.layers import trunc_normal_
from .modules.CBAM import CBAM


def to_3d(x):
    return rearrange(x, 'b c h w -> b (h w) c')


def to_4d(x, h, w):
    return rearrange(x, 'b (h w) c -> b c h w', h=h, w=w)



class OverlapPatchEmbed(nn.Module):
    # 4,in_c,480,640->4,embed_dim,480,640
    def __init__(self, in_c=3, embed_dim=64, bias=False, stride=1):
        super(OverlapPatchEmbed, self).__init__()

        self.proj = nn.Conv2d(in_c, embed_dim, kernel_size=3, stride=stride, padding=1, bias=bias)

    def forward(self, x):
        x = self.proj(x)

        return x

class BiasFree_LayerNorm(nn.Module):
    def __init__(self, normalized_shape):
        super(BiasFree_LayerNorm, self).__init__()
        if isinstance(normalized_shape, numbers.Integral):
            normalized_shape = (normalized_shape,)
        normalized_shape = torch.Size(normalized_shape)

        assert len(normalized_shape) == 1

        self.weight = nn.Parameter(torch.ones(normalized_shape))
        self.normalized_shape = normalized_shape

    def forward(self, x):
        sigma = x.var(-1, keepdim=True, unbiased=False)
        return x / torch.sqrt(sigma + 1e-5) * self.weight


class WithBias_LayerNorm(nn.Module):
    def __init__(self, normalized_shape):
        super(WithBias_LayerNorm, self).__init__()
        if isinstance(normalized_shape, numbers.Integral):
            normalized_shape = (normalized_shape,)
        normalized_shape = torch.Size(normalized_shape)

        assert len(normalized_shape) == 1

        self.weight = nn.Parameter(torch.ones(normalized_shape))
        self.bias = nn.Parameter(torch.zeros(normalized_shape))
        self.normalized_shape = normalized_shape

    def forward(self, x):
        mu = x.mean(-1, keepdim=True)
        sigma = x.var(-1, keepdim=True, unbiased=False)
        return (x - mu) / torch.sqrt(sigma + 1e-5) * self.weight + self.bias

class LayerNorm(nn.Module):
    def __init__(self, dim, LayerNorm_type):
        super(LayerNorm, self).__init__()
        if LayerNorm_type == 'BiasFree':
            self.body = BiasFree_LayerNorm(dim)
        else:
            self.body = WithBias_LayerNorm(dim)

    def forward(self, x):
        h, w = x.shape[-2:]
        return to_4d(self.body(to_3d(x)), h, w)

## Multi-DConv Head Transposed Self-Attention (MDTA)
class Attention(nn.Module):
    def __init__(self, dim, num_heads, bias):
        super(Attention, self).__init__()
        self.num_heads = num_heads
        self.temperature = nn.Parameter(torch.ones(num_heads, 1, 1))

        self.qkv = nn.Conv2d(dim, dim * 3, kernel_size=1, bias=bias)
        self.qkv_dwconv = nn.Conv2d(dim * 3, dim * 3, kernel_size=3, stride=1, padding=1, groups=dim * 3, bias=bias)
        self.project_out = nn.Conv2d(dim, dim, kernel_size=1, bias=bias)

    def forward(self, x):
        b, c, h, w = x.shape

        qkv = self.qkv_dwconv(self.qkv(x))
        q, k, v = qkv.chunk(3, dim=1)

        q = rearrange(q, 'b (head c) h w -> b head c (h w)', head=self.num_heads)
        k = rearrange(k, 'b (head c) h w -> b head c (h w)', head=self.num_heads)
        v = rearrange(v, 'b (head c) h w -> b head c (h w)', head=self.num_heads)

        q = torch.nn.functional.normalize(q, dim=-1)
        k = torch.nn.functional.normalize(k, dim=-1)

        attn = (q @ k.transpose(-2, -1)) * self.temperature
        attn = attn.softmax(dim=-1)

        out = (attn @ v)

        out = rearrange(out, 'b head c (h w) -> b (head c) h w', head=self.num_heads, h=h, w=w)

        out = self.project_out(out)
        return out

## Gated-Dconv Feed-Forward Network (GDFN)
class FeedForward(nn.Module):
    def __init__(self, dim, ffn_expansion_factor, bias):
        super(FeedForward, self).__init__()

        hidden_features = int(dim * ffn_expansion_factor)

        self.project_in = nn.Conv2d(dim, hidden_features * 2, kernel_size=1, bias=bias)

        self.dwconv = nn.Conv2d(hidden_features * 2, hidden_features * 2, kernel_size=3, stride=1, padding=1,
                                groups=hidden_features * 2, bias=bias)

        self.project_out = nn.Conv2d(hidden_features, dim, kernel_size=1, bias=bias)

    def forward(self, x):
        x = self.project_in(x)
        x1, x2 = self.dwconv(x).chunk(2, dim=1)
        x = F.gelu(x1) * x2
        x = self.project_out(x)
        return x

class TransformerBlock(nn.Module):
    def __init__(self, dim, num_heads, ffn_expansion_factor, bias, LayerNorm_type):
        super(TransformerBlock, self).__init__()

        self.norm1 = LayerNorm(dim, LayerNorm_type)
        self.attn = Attention(dim, num_heads, bias)
        self.norm2 = LayerNorm(dim, LayerNorm_type)
        self.ffn = FeedForward(dim, ffn_expansion_factor, bias)

    def forward(self, x):
        x = x + self.attn(self.norm1(x))
        x = x + self.ffn(self.norm2(x))

        return x

## Resizing modules
class Downsample(nn.Module):
    # 4,3,480,640->4,12,240,320
    def __init__(self, in_channel, n_feat):
        super(Downsample, self).__init__()

        self.body = nn.Sequential(nn.Conv2d(in_channel, n_feat // 2, kernel_size=3, stride=1, padding=1, bias=False),
                                  nn.PixelUnshuffle(2))

    def forward(self, x):
        return self.body(x)

class Down_wt(nn.Module):
    # [4,in_channel,120,160]->4,out_channel,60,80
    def __init__(self, in_ch, out_ch):
        super(Down_wt, self).__init__()
        self.wt = DWTForward(J=1, mode='zero', wave='haar')
        self.conv_bn_relu = nn.Sequential(
                                    nn.Conv2d(in_ch*4, out_ch, kernel_size=1, stride=1),
                                    nn.BatchNorm2d(out_ch),
                                    nn.ReLU(inplace=True),
                                    )
    def forward(self, x):
        yL, yH = self.wt(x)
        y_HL = yH[0][:,:,0,::]
        y_LH = yH[0][:,:,1,::]
        y_HH = yH[0][:,:,2,::]
        x = torch.cat([yL, y_HL, y_LH, y_HH], dim=1)
        x = self.conv_bn_relu(x)

        return x

class OverlapPatchEmbed_HWD(nn.Module):
    # 经过两次小波下采样，将数据从4,3,480,640化为4,64,120,160
    def __init__(self, in_ch, out_ch, bias=False, stride=1):
        super(OverlapPatchEmbed_HWD, self).__init__()
        self.proj = nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=stride, padding=1, bias=bias)
        self.down_wt = Down_wt(out_ch, out_ch)

    def forward(self, x):
        x = self.proj(x)
        x = self.down_wt(x)
        x = self.down_wt(x)

        return x
class VisionTransformer(nn.Module):
    def __init__(self,
                 input_channels=3,
                 dim=64,
                 num_blocks=[3,4,6,3],
                 num_refinement_block=2,
                 heads=[1,2,4,8],
                 ffn_expansion_factor=1,
                 bias=False,
                 LayerNorm_type='WithBias', # BiasFree
                 ):
        super(VisionTransformer, self).__init__()
        # self.patch_embed = OverlapPatchEmbed(input_channels,dim)
        # 将输入的4,3,480,640化为4,64,120,160
        # 第一层transformer
        self.patch_embed = OverlapPatchEmbed_HWD(in_ch=input_channels, out_ch=dim)
        self.res_down1 = nn.Conv2d(256,64, kernel_size=3, stride=1, padding=1, bias=False)
        # 将resnet的f0和第一层t的cat送入transformer
        self.encoder_1 = nn.Sequential(
            *[TransformerBlock(dim=dim*2,num_heads=heads[0],ffn_expansion_factor=ffn_expansion_factor,bias=bias,
                               LayerNorm_type=LayerNorm_type) for i in range(num_blocks[0])])

        # 第二次transformer
        self.res_down2 = nn.Conv2d(512, 128, kernel_size=3, stride=1, padding=1, bias=False)
        # self.down1_2 = Downsample(dim, dim)  ## From Level 1 to Level 2
        self.wt_down1 = Down_wt(128, 128) # B,C,120,160->B,C,60,80
        self.encoder_level2 = nn.Sequential(*[
            TransformerBlock(dim=dim*4, num_heads=heads[1], ffn_expansion_factor=ffn_expansion_factor,
                             bias=bias, LayerNorm_type=LayerNorm_type) for i in range(num_blocks[1])])

        # 第三层transformer
        self.res_down3 = nn.Conv2d(1024, 256, kernel_size=3, stride=1, padding=1, bias=False)
        # self.c_down2 = nn.Conv2d(128 + 1, 64, kernel_size=3, stride=1, padding=1, bias=False)
        # self.down2_3 = Downsample(dim * 2 + 32, dim * 2)  ## From Level 2 to Level 3
        self.wt_down2 = Down_wt(256, 256)
        self.encoder_level3 = nn.Sequential(*[
            TransformerBlock(dim=dim*8, num_heads=heads[2], ffn_expansion_factor=ffn_expansion_factor,
                             bias=bias, LayerNorm_type=LayerNorm_type) for i in range(num_blocks[2])])

        # 第四层transformer
        self.res_down4 = nn.Conv2d(2048, 512, kernel_size=3, stride=1, padding=1, bias=False)
        # self.c_down3 = nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1, bias=False)
        # self.down3_4 = Downsample(int(dim * 4 + 64), dim * 4)  ## From Level 3 to Level 4
        self.wt_down3 = Down_wt(512, 512)
        self.encoder_level4 = nn.Sequential(*[
            TransformerBlock(dim=dim*16, num_heads=heads[3], ffn_expansion_factor=ffn_expansion_factor,
                             bias=bias, LayerNorm_type=LayerNorm_type) for i in range(num_blocks[3])])

        # CBAM
        self.cbam0 = CBAM(channels=dim)   #64
        self.cbam1 = CBAM(channels=dim*4) # 256
        self.cbam2 = CBAM(channels=dim*8) # 512
        self.cbam3 = CBAM(channels=dim*16) #1024

    def init_weights(self, pretrained=None):
        """Initialize the weights in backbone.

        Args:
            pretrained (str, optional): Path to pre-trained weights.
                Defaults to None.
        """

        def _init_weights(m):
            if isinstance(m, nn.Linear):
                trunc_normal_(m.weight, std=.02)
                if isinstance(m, nn.Linear) and m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.LayerNorm):
                nn.init.constant_(m.bias, 0)
                nn.init.constant_(m.weight, 1.0)

        if isinstance(pretrained, str):
            self.apply(_init_weights)
            # logger = get_root_logger()
            load_checkpoint(self, pretrained, strict=False)
        elif pretrained is None:
            self.apply(_init_weights)
        else:
            raise TypeError('pretrained must be a str or None')

    def forward(self, imgs, f0, f1, f2, f3):

        # imgs [4,3,480,640],f0[4,256,120,160],f1[4,512,60,80],f2[4,1024,30,40],f3[4,2048,15,20]
        inputs = self.patch_embed(imgs)  #4,3,480,640->4,64,120,160
        res_feats1 = self.res_down1(f0)  # 4,256,120,160->4,64,120,160
        input_features_1 = torch.cat((inputs,res_feats1), dim=1) # 4,128,120,160

        # 第一层transformer
        out_encoder_level1 = self.encoder_1(input_features_1) # 4,128,120,160->4,128,120,160

        # 第二层transformer
        res_feats2 = self.res_down2(f1)  # 4，512,60,80->4,128,60,80
        t_feats1 = self.wt_down1(out_encoder_level1)  # 4,128,120,160->4,128,60,80
        input_features_2 = torch.cat((res_feats2,t_feats1), dim=1)  # 4,128,60,80,4,128,60,80->4,256,60,80
        out_encoder_level2 = self.encoder_level2(input_features_2) # 4,256,60,80->4,256,60,80
        out_encoder_level2 = out_encoder_level2+self.cbam1(out_encoder_level2)

        # 第三层transformer
        res_feats3 = self.res_down3(f2) #4,1024,30,40->4,512,30,40
        t_feats2 = self.wt_down2(out_encoder_level2) # 4,512,60,80->4,512,30,40
        input_features_3 = torch.cat((res_feats3,t_feats2), dim=1) # 4,512,30,40,4,512,30,40->4,512,30,40
        out_encoder_level3 = self.encoder_level3(input_features_3) # 4,512,30,40->4,512,30,40
        out_encoder_level3 = out_encoder_level3+self.cbam2(out_encoder_level3)

        # 第四层transformer
        res_feats4 = self.res_down4(f3) # 4,2048,15,20->4,512,15,20
        t_feats3 = self.wt_down3(out_encoder_level3)  # 4,512,30,40->4,512,15,20
        input_features_4 = torch.cat((res_feats4,t_feats3), dim=1) # 4,512,15,20,4,512,15,20->4,1024,15,20
        out_encoder_level4 = self.encoder_level4(input_features_4) # 4,1024,15,20->4,1024,15,20
        out_encoder_level4 = out_encoder_level4+self.cbam3(out_encoder_level4)

        return out_encoder_level1, out_encoder_level2, out_encoder_level3, out_encoder_level4




if __name__ == '__main__':
    x = torch.randn(4,3,480,640)
    f0 = torch.randn(4,256,120,160)
    f1 = torch.randn(4,512,60,80)
    f2 = torch.randn(4,1024,30,40)
    f3 = torch.randn(4,2048,15,20)
    model = VisionTransformer(input_channels=3,dim=64)
    out1,out2,out3,out4 = model(x,f0,f1,f2,f3)
    print(out1.size())
    print(out2.size())
    print(out3.size())
    print(out4.size())






























        #
        # inputs = torch.cat([imgs, feats], dim=1)
        # H,W = imgs.shape[2:]
        #
        # inputs_encoder_1 = self.patch_embed(inputs)
        # out_enc_level1 = self.encoder_level1(inputs_encoder_1)
        #
        # f1 = self.c_down1(torch.cat([inputs_encoder_1,feats[1]],dim=1))
        # inp_enc_level2 = self.down1_2(out_enc_level1)
        # inp_enc_level2 = self.SpecFreq2(inp_enc_level2, spatial_size=(H // 2, W // 2))
        # out_enc_level2 = self.encoder_level2(torch.cat([inp_enc_level2, f1], 1))
        # if self.with_cbam:
        #     out_enc_level2 = out_enc_level2 + self.cbam_2(out_enc_level2)
        #
        # f2 = self.c_down2(torch.cat([ feats[2], feats[2]], 1))
        # inp_enc_level3 = self.down2_3(out_enc_level2)
        # inp_enc_level3 = self.SpecFreq3(inp_enc_level3, spatial_size=(H // 4, W // 4))
        # out_enc_level3 = self.encoder_level3(torch.cat([inp_enc_level3, f2], 1))
        # if self.with_cbam:
        #     out_enc_level3 = out_enc_level3 + self.cbam_3(out_enc_level3)
        #
        # f3 = self.c_down3(torch.cat([feats[3], feats[3]], 1))
        # inp_enc_level4 = self.down3_4(out_enc_level3)
        # latent = self.latent(torch.cat([inp_enc_level4, f3], 1))
        #
        # inp_dec_level3 = self.up4_3(latent)
        # inp_dec_level3 = torch.cat([inp_dec_level3, out_enc_level3], 1)
        # inp_dec_level3 = self.reduce_chan_level3(inp_dec_level3)
        # out_dec_level3 = self.decoder_level3(inp_dec_level3)
        #
        # inp_dec_level2 = self.up3_2(out_dec_level3)
        # inp_dec_level2 = torch.cat([inp_dec_level2, out_enc_level2], 1)
        # inp_dec_level2 = self.reduce_chan_level2(inp_dec_level2)
        # out_dec_level2 = self.decoder_level2(inp_dec_level2)
        #
        # inp_dec_level1 = self.up2_1(out_dec_level2)
        # inp_dec_level1 = torch.cat([inp_dec_level1, out_enc_level1], 1)
        # out_dec_level1 = self.decoder_level1(inp_dec_level1)
        #
        # out_dec_level1 = self.refinement(out_dec_level1)
        # out_dec_level1 = self.output(out_dec_level1)
        #
        # return torch.sigmoid(out_dec_level1)
